package com.whxy02.tolearn.app.controller;

import com.whxy02.tolearn.app.beans.User;
import com.whxy02.tolearn.app.resultObject.ResultState;
import com.whxy02.tolearn.app.service.UserService;
import com.whxy02.tolearn.app.utils.Md5Util;
import com.whxy02.tolearn.app.utils.PackResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/login")
public class Login {

    @Resource
    UserService userService;
    @Resource
    PackResult packResult;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    Md5Util md5Util;

    @RequestMapping("/check")
    public Map<String,Object> check(@RequestParam("username")String username, @RequestParam("pwd")String pwd, HttpServletRequest request, HttpServletResponse response){
        HttpSession session=request.getSession();

        try {
            User u=userService.getByName(username);
            if(u!=null){
                if(u.getPwd().equals(md5Util.md5(pwd))){
                    Cookie cookie=new Cookie("sessionID",session.getId());
                    cookie.setMaxAge(60*30);
                    cookie.setPath("/");
                    response.addCookie(cookie);

                    redisTemplate.opsForValue().set("user:"+session.getId(),u,60*30, TimeUnit.SECONDS);
                    response.sendRedirect(request.getContextPath()+"/index.html");
//                    session.setAttribute("user",u);
//                    session.setMaxInactiveInterval(60*30);
                    return packResult.doPackToMap(ResultState.SUCCESS);
                }

                else{
                    return packResult.doPackToMap(ResultState.PASSWORD_ERR);
                }


            }

            else{
                return packResult.doPackToMap(ResultState.USER_NOT_FOUND);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return packResult.doPackToMap(ResultState.SERVER_ERR);
        }
    }

    /**
     * 注销
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/logout")
    public Map<String,Object> logout(HttpServletRequest request,HttpServletResponse response){
        Cookie[] cookies=request.getCookies();
        for(Cookie cookie:cookies){
            if(cookie.getName().equals("sessionID")) {
                redisTemplate.expire("user:"+cookie.getValue(),0,TimeUnit.SECONDS);
                cookie.setMaxAge(1);
                response.addCookie(cookie);
                try {
                    response.sendRedirect(request.getContextPath()+"/index.html");
                } catch (IOException e) {

                    e.printStackTrace();
                }
                return packResult.doPackToMap(ResultState.SUCCESS);
            }
        }
//        try {
//            session.removeAttribute("user");
//            session.setMaxInactiveInterval(1);
//            return packResult.doPackToMap(ResultState.SUCCESS);
//        }catch (Exception e) {
//
//
//        }
        try {
            response.sendRedirect(request.getContextPath()+"/index.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return packResult.doPackToMap(ResultState.LOGIN_TIMEOUT);
    }

}
